package com.oncliedisk.administrator.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.oncliedisk.administrator.Base.BaseActivity;
import com.oncliedisk.administrator.DB.Dao;
import com.oncliedisk.administrator.content.FileConfig;
import com.oncliedisk.administrator.model.EntryModel;
import com.oncliedisk.administrator.onlinedisk.R;


/**
 * Created by Administrator on 2015/12/4.
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener {

    private ImageView img_back; //返回图片
    private ImageView img_search; //搜索图片
    private EditText content; //搜索关键字
    private TextView reslut; //结果
    private LinearLayout searchLayout; //搜索
    private RelativeLayout relaiveLayout;
    private LinearLayout layout; //空的图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        search_result.setVisibility(View.VISIBLE);
        isSearch = true;
        initWeight();
        initListener();
    }

    public void initWeight() {
        img_back = (ImageView) findViewById(R.id.search_back);
        img_search = (ImageView) findViewById(R.id.search_action);
        content = (EditText) findViewById(R.id.search_content);
        reslut = (TextView) findViewById(R.id.search_result);
        searchLayout = (LinearLayout) findViewById(R.id.search_layout);
        relaiveLayout = (RelativeLayout) findViewById(R.id.base_layout_title);
        layout = (LinearLayout) findViewById(R.id.search_null);
        searchLayout.setVisibility(View.VISIBLE);
        relaiveLayout.setVisibility(View.GONE);
    }

    private void initListener() {
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //返回
                finish();
            }
        });
        img_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //搜索
                String name = content.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(SearchActivity.this, "关键字不可为空!", Toast.LENGTH_SHORT).show();
                } else {
                    //如果有输入关键字
                    searchByName(name);
                }
            }
        });
    }

    /**
     * 根据关键字进行查询有关键字的数据
     *
     * @param name //
     */
    private void searchByName(String name) {
        initAdapter(Dao.getInstance(SearchActivity.this).getAllFile("filename like ?", new String[]{"%" + name + "%"}));
        listView.setAdapter(adapter);
        if (adapter.getData().size() <= 0) {
            layout.setVisibility(View.VISIBLE);
        } else {
            layout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //根据点击的条目，进行跳转页面显示
        EntryModel item = (EntryModel) adapter.getItem(position);
        if (isMulChoice == true) {
            //如果现在是长按状态,则点击的这个设置成选中状态
            item.setIsCheck(!item.isCheck());
            updateAdapter();
            return;
        }
        String type = item.getMimeType();
        if (type.equals(FileConfig.TYPE_FOLDER)) {
            //如果是文件夹,则打开文件夹
            Intent intent = new Intent(SearchActivity.this, ShowFolder.class);
            EntryModel model = (EntryModel) adapter.getItem(position);
            intent.putExtra("data", model);
            startActivity(intent);
        } else {
            openFile(item);
        }
    }
}
